home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2841 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: sscanf problems
  5. Date: 23 Jan 1996 22:50 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <23JAN199622501430@erich.triumf.ca>
  9. References: <4e4c2v$j2g@mathserv.mps.ohio-state.edu> <4e4h0j$af7@fountain.mindlink.net>
  10. NNTP-Posting-Host: ftp.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <4e4h0j$af7@fountain.mindlink.net>, genew@mindlink.bc.ca (Gene Wirchenko) writes...
  14. >Chris Mongold <cmongold@magnus.acs.ohio-state.edu> wrote:
  15. >>Hello,
  16. >>    I'm sorry if this is an inappropriate topic, but I've tried
  17. >>everything else.  I can't seem to get sscanf to to separate a string
  18. >>into various variable types.  Here is an example:
  19.  
  20. >>#include <stdio.h>
  21.  
  22. ...
  23. >>char input[20], crap[17], segment[6], seg_len[3], begin[3], load[3];
  24. >>int type;
  25. >>FILE *fp;
  26. >>printf("File: ");
  27. >>gets(input);
  28. >     Buffer can be overflowed.  You might want to use fgets().
  29.  
  30. You _do_ want to use fgets() - gets() is evil!
  31.  
  32. .. snip, snip....
  33.  
  34. >>fgets(crap, 17, fp);
  35. >>sscanf(crap, "%d%3s%6s%3s%3s", type, begin, segment, seg_len, load);
  36.                                  ^^^ This must be &type - you must give scanf()
  37. the _address_ of (or pointer to) the location it is to store input.  The name
  38. of a char array acts as a pointer to the first location of the array, but you
  39. must use the "&" operator on ints and similar things.
  40.  
  41. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  42. Internet: bennett@triumf.ca         | of one another only when one can be
  43. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  44. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  45. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  46.  
  47.  
  48.  
  49.  
  50.  
  51.